﻿using System;
using System.Collections.Generic;
using System.IO;
using Apache.Cassandra;
using SKBKontur.Cassandra.Logging.Model;
using SKBKontur.Cassandra.Logging.Storage;
using SKBKontur.Cassandra.Proxy;
using SKBKontur.Cassandra.Proxy.Management;
using SKBKontur.Logging;

namespace ReadToJSON
{
    // todo Написать нормальную тулзу
    public static class EntryPoint
    {
        static void Main(/*string[] args*/)
        {
            var dt1 = new DateTime(2011, 08, 26, 0, 0, 0, 0);//DateTime.Parse(args[0]);
            var dt2 = new DateTime(2011, 08, 27, 0, 0, 0, 0);//DateTime.Parse(args[1]);

            var args = new string[3];
            args[2] = "Log.log";

            FileStream strm;
            if (File.Exists(args[2]))
            {
                strm = new FileStream(args[2], FileMode.Truncate);
                Console.WriteLine("File {0} was cleaned", args[2]);
            }
            else
            {
                strm = new FileStream(args[2], FileMode.Create);
                Console.WriteLine("File {0} was create", args[2]);
            }

            int counter = 0;
            var writer = new JsonLogEventWriter("\r\n", strm);
            foreach (var e in GetLogEvents(dt1, dt2))
            {
                writer.Create(e);
                counter++;
            }
            Console.WriteLine("In file was write {0} objects in JSON-notation", counter);
        }

        private static IEnumerable<log4netEvent> GetLogEvents(DateTime to, DateTime from)
        {
            var Conf = new ConfigurationInfo("Log", "Master", "Exception", "Info", "Warn", "Debug", "Error", "Fatal",
                                             0, 0, 0, 0, 0, 0);
            var configurator = new SimpleCassandraLoggingConfigurator(Conf, new ConsoleLevelDispatchingLogEventWriterConfigurator("\r\n"),
                                          new ProxyFactory("node1", 9160),
                                          new ProxyFactory("node2", 9160),
                                          new ProxyFactory("node3", 9160));
            var logEventReader = new CassandraLogEventReader(configurator);
            return logEventReader.Read<log4netEvent>(to, from);
        }
    }
}
